//
//  LSZReportViewController.m
//  JKFuYinProject
//
//  Created by 九酷 on 2018/8/30.
//  Copyright © 2018年 LC. All rights reserved.
//

#import "LSZReportViewController.h"

@interface LSZReportViewController ()<UITableViewDelegate,UITableViewDataSource,UITextViewDelegate>
@property(nonatomic,strong) UITableView * tableViewReport;
@property(nonatomic,strong) LSZReportTypeManager * reportTypeManager;
@property(nonatomic,assign) NSInteger chooseRow;
@property(nonatomic,strong) NSString * kind;//举报id
@property(nonatomic,strong) NSString * stringContent;//输入的内容
@property(nonatomic,strong) TPKeyboardAvoidingScrollView * backScrollView;
@end

@implementation LSZReportViewController
#pragma mark - 得到举报的类型
-(void)typeOfReport
{
    [LSZRequestResolutionDic initWithTypeOfReport:^(BOOL isOk, NSString * string, LSZReportTypeManager * manager) {
        if (isOk == YES) {
            self.reportTypeManager = manager;
            [self.tableViewReport reloadData];
        }
    }];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self typeOfReport];
}
- (void)viewDidLoad {
    [super viewDidLoad];

    self.backScrollView = [[TPKeyboardAvoidingScrollView alloc] initWithFrame:CGRectMake(0,0,ScreenWidth,ScreenHeight)];
    self.backScrollView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    self.backScrollView.userInteractionEnabled = YES;
    [self.view addSubview:self.backScrollView];

    self.chooseRow = -1;
    self.title = @"举报";
    [self.tableViewReport registerClass:[LSZSaveAddressFootCell class] forHeaderFooterViewReuseIdentifier:@"LSZSaveAddressFootCell"];
    [self.tableViewReport registerClass:[LSZHeaderTitleCell class] forHeaderFooterViewReuseIdentifier:@"LSZHeaderTitleCell"];
    [self.tableViewReport registerClass:[LSZContentReportTableViewCell class] forCellReuseIdentifier:@"LSZContentReportTableViewCell"];
    [self.tableViewReport registerClass:[LSZTextViewTableViewCell class] forCellReuseIdentifier:@"LSZTextViewTableViewCell"];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 45;
    }else if(indexPath.section == 2){
        return 100;
    }
    else{

        return 50;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 0||section == 1) {
        return 0.001;
    }else{
        return 90;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0||section == 2) {
        return 10;
    }else{
        CGFloat font = [LSZSpecialPackaging initWithReturnsthesizeOf:13.5 :0.5];
        CGFloat height = [LSZSpecialPackaging caculateHeightOfText:ScreenWidth-20 height:ScreenHeight font:font :[NSString stringWithFormat:@"内容:%@",self.titleString]];
        return height+20;
    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0||section == 2) {
        return 1;
    }else{
        return self.reportTypeManager.arrayData.count;
    }
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0 ||section == 2) {
        UITableViewHeaderFooterView * cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"cell"];
        if (!cell ) {
            cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"cell"];
        }
        return cell;
    }else{

        LSZHeaderTitleCell * cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"LSZHeaderTitleCell"];
        cell.labelTitle.text = [NSString stringWithFormat:@"内容:%@",self.titleString];
        CGFloat font = [LSZSpecialPackaging initWithReturnsthesizeOf:13.5 :0.5];
        CGFloat height = [LSZSpecialPackaging caculateHeightOfText:ScreenWidth-20 height:ScreenHeight font:font :[NSString stringWithFormat:@"内容:%@",self.titleString]];
        CGRect rect = cell.labelTitle.frame;
        rect.size.height = height;
        cell.labelTitle.frame = rect;
        return cell;
    }
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section == 0||section  == 1) {
        UITableViewHeaderFooterView * cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"cell"];
        if (!cell ) {
            cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"cell"];
        }
        return cell;
    }else{
        LSZSaveAddressFootCell * cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"LSZSaveAddressFootCell"];
        [cell.buttonSave addTarget:self action:@selector(clickSave) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
        cell.textLabel.text = [NSString stringWithFormat:@"用户:%@",self.name];
        [LSZSpecialPackaging setLabelAdaptiveSize:cell.textLabel font:13.5];
        cell.textLabel.textColor = titleColorLSZ;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else if (indexPath.section == 2){
        LSZTextViewTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZTextViewTableViewCell" forIndexPath:indexPath];
        cell.textViewContent.delegate = self;
        if ((NullStringjudgment(NSStr(self.stringContent))).length == 0) {
            cell.textViewContent.text = @"请详细填写,以确保举报能够被受理!";
        }else{
            cell.textViewContent.text = self.stringContent;
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    else{
        LSZContentReportTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZContentReportTableViewCell" forIndexPath:indexPath];
        LSZReportTypeMg * mg = nil;
        mg = self.reportTypeManager.arrayData[indexPath.row];
        cell.labelTitle.text = mg.name;
        if (self.chooseRow == indexPath.row) {
            cell.imageViewChoose.image = [UIImage imageNamed:@"选中"];
        }else{
            cell.imageViewChoose.image = [UIImage imageNamed:@"未选中"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section  == 1) {
        self.chooseRow = indexPath.row;
        LSZReportTypeMg * mg = nil;
        mg = self.reportTypeManager.arrayData[indexPath.row];
        self.kind = mg.id_id;
        [self.tableViewReport reloadData];
    }
}
#pragma mark - 举报
-(void)clickSave
{
    if ([[_UserM usercode] isEqualToString:@""]) {
        pushAccLoginVC;
    }else{
//        LSZTextViewTableViewCell * cell = (LSZTextViewTableViewCell *)[self.tableViewReport cellForRowAtIndexPath:[NSIndexPath indexPathForRow:self.reportTypeManager.arrayData.count  inSection:1]];
        if (self.chooseRow == -1) {
            [self initWithtring:@"请选择举报类型!"];
            return;
        }
        [LSZRequestData contentOfTheReport:self.tid type:self.type kind:self.kind comment_id:self.comment_id content:self.stringContent :^(BOOL isOK, NSString * string, NSDictionary * dic) {
            [self initWithtring:string];
            if (isOK == YES) {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    // 刷新表格  和结束刷新
                    [self.navigationController popViewControllerAnimated:YES];
                });
            }else{
             }
        }];
    }
}
#pragma mark - UITextViewDelegate
- (void)textViewDidEndEditing:(UITextView *)textView
{
    if(textView.text.length < 1){
        textView.text = @"请详细填写,以确保举报能够被受理!";
        textView.textColor = [UIColor grayColor];
    }
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if([textView.text isEqualToString:@"请详细填写,以确保举报能够被受理!"]){
        textView.text=@"";
        textView.textColor=[UIColor blackColor];
    }
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if ([text isEqualToString:@"\n"]){ //判断输入的字是否是回车，即按下return
        //在这里做你响应return键的代码
        [self.view endEditing:YES];
        return NO; //这里返回NO，就代表return键值失效，即页面上按下return，不会出现换行，如果为yes，则输入页面会换行
    }
    NSString * resultStr = [textView.text stringByAppendingString:text];
    self.stringContent = resultStr;
    return YES;
}
-(UITableView *)tableViewReport
{
    if (_tableViewReport == nil) {
        _tableViewReport = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-TAB_BAR_HEIGHT) style:UITableViewStyleGrouped];
        _tableViewReport.delegate = self;
        _tableViewReport.dataSource = self;
        _tableViewReport.separatorColor = divider;
        _tableViewReport.estimatedRowHeight =0;
        _tableViewReport.estimatedSectionFooterHeight =0;
        _tableViewReport.estimatedSectionHeaderHeight =0;
        _tableViewReport.separatorInset = UIEdgeInsetsMake(0, 20, 0, 20);
        _tableViewReport.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.backScrollView addSubview:_tableViewReport];
    }
    return _tableViewReport;
}

@end
